#include "mainwindow.h"
#include "ui_mainwindow.h"
#include <QDebug>



MainWindow::MainWindow(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::MainWindow)
{
    ui->setupUi(this);

    scene = new QGraphicsScene(this);
    ui->graphicsView->setScene(scene);


    addBorder();


    // Create the button, make "this" the parent
    m_buttonClear = new QPushButton("Clear", this);
    // set size and location of the button
    m_buttonClear->setGeometry(QRect(QPoint(950 , 572),
                                    QSize(191, 61)));
    // Connect button signal to appropriate slot
       connect(m_buttonClear, SIGNAL(released()), this, SLOT(handleButton()));



    // Create the button, make "this" the parent
    m_buttonGrid = new QPushButton("Grid", this);
    // set size and location of the button
    m_buttonGrid->setGeometry(QRect(QPoint(950 , 472),
                                       QSize(191, 61)));
    // Connect button signal to appropriate slot
          connect(m_buttonGrid, SIGNAL(released()), this, SLOT(handleButtonGrid()));


    // Create the button, make "this" the parent
    m_buttonGrid = new QPushButton("Diagonal", this);
    // set size and location of the button
    m_buttonGrid->setGeometry(QRect(QPoint(950 , 372),
                                             QSize(191, 61)));
    // Connect button signal to appropriate slot
    connect(m_buttonGrid, SIGNAL(released()), this, SLOT(handleButtonDiagonal()));


    // Create the button, make "this" the parent
    m_buttonSpiral = new QPushButton("Spiral", this);
    // set size and location of the button
    m_buttonSpiral->setGeometry(QRect(QPoint(950 , 272),
                                             QSize(191, 61)));
    // Connect button signal to appropriate slot
    connect(m_buttonSpiral, SIGNAL(released()), this, SLOT(handleButtonSpiral()));


    // Create the button, make "this" the parent
    m_buttonCorridor = new QPushButton("Corridor", this);
    // set size and location of the button
    m_buttonCorridor->setGeometry(QRect(QPoint(950 , 172),
                                             QSize(191, 61)));
    // Connect button signal to appropriate slot
    connect(m_buttonCorridor, SIGNAL(released()), this, SLOT(handleButtonCorridor()));



    timer2 = new QTimer(this);

    // create a timer


    i = 0;
    j = 0;
    z = 0;
    x = 0;


    radius = 0;
    circleRadius =0;
    elapsed = 0;
    r = elapsed /1000;

    //connect(timer2, SIGNAL(timeout()), this, SLOT(draw()));


}

MainWindow::~MainWindow()
{
    delete ui;
}

void MainWindow::handleButton()
{

    scene->clear();
}


void MainWindow::handleButtonGrid()
{
    resetVar();

    connect(timer2, SIGNAL(timeout()), this, SLOT(drawGrid()));
    // msec
    timer2->start(5);
}

void MainWindow::handleButtonDiagonal()
{
    resetVar();

    connect(timer2, SIGNAL(timeout()), this, SLOT(drawDiagonal()));
    // msec
    timer2->start(100);
}

void MainWindow::handleButtonSpiral()
{
    resetVar();

    connect(timer2, SIGNAL(timeout()), this, SLOT(drawSpiral()));
    // msec
    timer2->start(100);
}

void MainWindow::handleButtonCorridor()
{
    resetVar();

    connect(timer2, SIGNAL(timeout()), this, SLOT(drawCorridor()));
    // msec
    timer2->start(1);
}



void MainWindow::drawGrid()
 {
     QBrush greenBrush(Qt::green);
     QBrush blueBrush(Qt::blue);
     QPen outlinePen(Qt::black);

     /*
     for(int i = 0;i <= 500; i = i +10){

         for(int j = 0;j <= 500; j = j +10)
         {
             rectangle = scene->addRect( QRectF(i, j, 1, 1), outlinePen, blueBrush);
         }

     }
     */



     //middle Rect
     //rectangle = scene->addRect( QRectF(100+i,100+ j, 1, 1), outlinePen, blueBrush);

     // outsidt Rect
     //rectangle = scene->addRect( QRectF(i,j, 1, 1), outlinePen, blueBrush);

     // inner Rect
     rectangle = scene->addRect( QRectF(200+i,200+j, 1, 1), outlinePen, blueBrush);

     i +=10;


     //if (i % 300 == 0 ) //middle Rect
     //if (i % 100 == 0 ) // inner Rect
     //if (i % 500 == 0 ) // outside Rect
     if (i % 100 == 0 ) // inner Rect
     {
         j += 10;
         i = 0;
         z++;
     }

     qDebug() << "Draw Grid...\n";
     qDebug() << i ;
     qDebug() << "\n";
     qDebug() << j ;
     qDebug() << "\n";
     qDebug() << z ;
     qDebug() << "\n";

     // if (z  == 30) // middel Rect
     //if (z  == 50) // outside Rect
     if (z  == 10) // inner Rect
        timer2->stop();

 }

void MainWindow::drawDiagonal()
 {
     QBrush greenBrush(Qt::green);
     QBrush blueBrush(Qt::blue);
     QPen outlinePen(Qt::black);


     rectangle = scene->addRect( QRectF(i, i, 1, 1), outlinePen, blueBrush);

     i +=10;

     qDebug() << "Draw Diagonal...\n";
     qDebug() << i ;
     qDebug() << "\n";
     qDebug() << j ;
     qDebug() << "\n";
     qDebug() << z ;
     qDebug() << "\n";


     if (i  == 500)
        timer2->stop();

 }

void MainWindow::drawSpiral()
 {
/*





     for (int i = 0; i < n; ++i) {
         painter->rotate(30);
         qreal radius = 0 + 120.0*((i+r)/n);
         qreal circleRadius = 1 + ((i+r)/n)*20;
         painter->drawEllipse(QRectF(radius, -circleRadius,
                                     circleRadius*2, circleRadius*2));
     }
*/

    QBrush greenBrush(Qt::green);
    QBrush blueBrush(Qt::blue);
    QPen outlinePen(Qt::black);


     i +=1;


     radius = 0 + 120.0*((i+r)/n);
     circleRadius = 1 + ((i+r)/n)*20;

      rectangle = scene->addRect( QRectF(radius, -circleRadius, 1, 1), outlinePen, blueBrush);



      qDebug() << "Draw Diagonal...\n";
      qDebug() << i ;
      qDebug() << "\n";
      qDebug() << radius ;
      qDebug() << "\n";
      qDebug() << circleRadius ;
      qDebug() << "\n";


     if (i  == 500)
        timer2->stop();

 }


void MainWindow::drawCorridor()
 {
    QBrush greenBrush(Qt::green);
    QBrush blueBrush(Qt::blue);
    QPen outlinePen(Qt::black);

 qDebug() << "------------------------------------------------------------------------------------\n";

    qDebug() << "Draw Corridor...\n";
    qDebug() << i ;
    qDebug() << "\n";
    qDebug() << j ;
    qDebug() << "\n";
    qDebug() << x;
    qDebug() << "\n";
    qDebug() << z;
    qDebug() << "\n";




    i += 10;




    rectangle = scene->addRect( QRectF(x + i, j, 1, 1), outlinePen, blueBrush);


    if (i % 50 == 0)
    {
        i = 0;
        x += 10;
        j += 10;
        z++;

    }

    qDebug() << "Draw Corridor...\n";
    qDebug() << i ;
    qDebug() << "\n";
    qDebug() << j ;
    qDebug() << "\n";
    qDebug() << x;
    qDebug() << "\n";
    qDebug() << z;
    qDebug() << "\n";

qDebug() << "------------------------------------------------------------------------------------\n";

     if (z  == 50)
        timer2->stop();

 }



void MainWindow::resetVar()
{

    i = 0;
    j = 0;
    z = 0;
}

void MainWindow::addBorder()
{

    QBrush greenBrush(Qt::green);
    QBrush blueBrush(Qt::blue);
    QPen outlinePen(Qt::black);

    line = scene->addLine(QLineF(0, 0, 0, 500),outlinePen);
    line = scene->addLine(QLineF(0, 0, 500, 0),outlinePen);
    line = scene->addLine(QLineF(500, 0, 500, 500),outlinePen);
    line = scene->addLine(QLineF(0, 500, 500, 500),outlinePen);


    line = scene->addLine(QLineF(100, 100, 100, 400),outlinePen);
    line = scene->addLine(QLineF(100, 100, 400, 100),outlinePen);
    line = scene->addLine(QLineF(400, 100, 400, 400),outlinePen);
    line = scene->addLine(QLineF(100, 400, 400, 400),outlinePen);

    line = scene->addLine(QLineF(200, 200, 200, 300),outlinePen);
    line = scene->addLine(QLineF(200, 200, 300, 200),outlinePen);
    line = scene->addLine(QLineF(300, 200, 300, 300),outlinePen);
    line = scene->addLine(QLineF(200, 300, 300, 300),outlinePen);

}
